<?php

namespace Model\Room;

use Extension\Tools;
use Model\RoomBase;
use Model\User;
use Handler\ZoneHandler;

class RoomMain extends RoomBase {
	/**
	 * 当前状态
	 *
	 * @var int
	 */
	private $m_CurStatus;
	/**
	 * 计时器
	 *
	 * @var int
	 */
	private $m_CurTime;
	/**
	 * 请求处理
	 *
	 * @var ClientRequest
	 */
	private $m_ClientRequest;
	/**
	 * 逻辑算法
	 *
	 * @var Algorithm
	 */
	private $m_Algo;
	/**
	 * 回合数
	 *
	 * @var unknown
	 */
	private $m_Round;
	/**
	 * 总玩家抽水
	 *
	 * @var unknown
	 */
	private $m_TotalUserChouShui;
	/**
	 * 总机器人抽水
	 *
	 * @var unknown
	 */
	private $m_TotalRobotChouShui;
	/**
	 * 玩家输赢
	 *
	 * @var unknown
	 */
	private $m_TotalWin;
	/**
	 * 控杀ID
	 *
	 * @var unknown
	 */
	private $m_ControlId;
	/**
	 * 控杀难度
	 *
	 * @var unknown
	 */
	private $m_ControlDifficulty;
	/**
	 * 最大机器人个数
	 *
	 * @var int
	 */
	private $m_MaxRobotCount;
	/**
	 * 是否要准备轮回局数
	 *
	 * @var unknown
	 */
	private $m_IsResetRound;
	/**
	 */
	function __construct($roomId, $zoneId, $roomNum, $name, $type, $diFen, $minEnter, $maxEnter, $chouShui, $difficulty) {
		$this->m_RoomId = $roomId;
		$this->m_ZoneId = $zoneId;
		$this->m_RoomNum = $roomNum;
		$this->m_RoomName = $name;
		$this->m_RoomType = $type;
		
		if ($this->m_RoomType == RoomBase::TYPE_FREE) {
			$roomData = ZoneHandler::mb_GetInstance ()->mb_GetRoomInfo ( $zoneId, $roomNum );
			if ($roomData != null) {
				$chouShui = $roomData ['choushui'];
				$difficulty = $roomData ['difficulty'];
				
				$this->m_Round = $roomData ['round'];
				$this->m_TotalUserChouShui = $roomData ['totaluserchoushui'];
				$this->m_TotalRobotChouShui = $roomData ['totalrobotchoushui'];
				$this->m_TotalWin = $roomData ['totalwin'];
				$this->m_ControlId = $roomData ['controlid'];
				$this->m_ControlDifficulty = $roomData ['controldifficulty'];
				$this->m_MaxRobotCount = $roomData ['robotcount'];
			} else {
				$this->m_Round = 0;
				$this->m_TotalUserChouShui = 0;
				$this->m_TotalRobotChouShui = 0;
				$this->m_TotalWin = 0;
				$this->m_ControlId = '';
				$this->m_ControlDifficulty = 1;
				$this->m_MaxRobotCount = mt_rand ( 2, Algorithm::MAXUSER - 4 );
				ZoneHandler::mb_GetInstance ()->mb_CreateOrUpdateRoomInfo ( $this->m_ZoneId, $this->m_RoomNum, $this->m_Round, $difficulty, $chouShui, $this->m_TotalUserChouShui, $this->m_TotalRobotChouShui, $this->m_TotalWin, $this->m_ControlId, $this->m_ControlDifficulty, $this->m_MaxRobotCount );
			}
		}
		
		$this->m_Algo = new Algorithm ( $this, $diFen, $minEnter, $maxEnter, $chouShui, $difficulty );
		$this->m_ClientRequest = new ClientRequest ( $this, $this->m_Algo );
		$this->m_IsResetRound = false;
		
		Tools::mb_LogResult ( "$name 初始化成功" );
	}
	/**
	 */
	function __destruct() {
	}
	// -------------------------------------------继承----------------------------------------//
	/**
	 * 房间消息入口
	 *
	 * {@inheritdoc}
	 *
	 * @see \Model\RoomBase::mb_DataIn()
	 */
	public function mb_DataIn($user, $data) {
		$method = $data->method;
		call_user_func_array ( array (
				$this->m_ClientRequest,
				$method 
		), array (
				$user,
				$data 
		) );
	}
	/**
	 * 玩家进入
	 *
	 * {@inheritdoc}
	 *
	 * @see \Model\RoomBase::mb_UserIn()
	 */
	public function mb_UserIn($user) {
		if ($this->m_Algo->mb_AddPlayer ( $user )) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 玩家退出
	 *
	 * {@inheritdoc}
	 *
	 * @see \Model\RoomBase::mb_UserOut()
	 */
	public function mb_UserOut($user) {
		return $this->m_Algo->mb_RemovePlayer ( $user );
	}
	// -------------------------------------------方法----------------------------------------//
	/**
	 * 同步信息
	 *
	 * @param User $user        	
	 */
	public function mb_GetSyncData() {
		$players = $this->m_Algo->mb_GetAllPlayers ();
		$pTemp = array ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] )) {
				$pTemp [] = $players [$i]->mb_GetSyncData ();
			} else {
				$pTemp [] = null;
			}
		}
		$data = array (
				'roomname' => ( string ) $this->m_RoomName,
				'difen' => ( string ) $this->m_Algo->m_DiFen,
				'status' => $this->m_CurStatus,
				'time' => $this->m_CurTime,
				'round' => ( int ) $this->m_Round,
				'players' => $pTemp 
		);
		return $data;
	}
	public function mb_GetRoomInfo() {
		$players = $this->m_Algo->mb_GetAllPlayers ();
		$playerData = array ();
		
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] )) {
				$data = array (
						'name' => $players [$i]->m_User->m_Name,
						'nick' => $players [$i]->m_User->m_Nick,
						'coin' => ( double ) $players [$i]->m_User->m_Coin 
				);
				$playerData [] = $data;
			} else {
				$playerData [] = null;
			}
		}
		
		return array (
				'id' => $this->m_RoomId,
				'name' => $this->m_RoomName,
				'difen' => $this->m_Algo->m_DiFen,
				'players' => $playerData 
		);
	}
	public function mb_RobotAutoEnterRoom() {
		if ($this->m_RoomType != RoomBase::TYPE_FREE)
			return;
		
		$robotCount = $this->m_Algo->mb_GetPlayerCount () - $this->m_Algo->mb_GetRealPlayerCount ();
		if ($this->m_Algo->mb_GetPlayerCount () < Algorithm::MAXUSER && $robotCount < $this->m_MaxRobotCount) {
			$addCount = $this->m_MaxRobotCount - $robotCount;
			Tools::mb_LogResult ( "准备加入一个机器人" );
			for($i = 0; $i < $addCount; $i ++) {
				if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
					if (mt_rand ( 0, 1000 ) < 850) {
						
						Tools::mb_LogResult ( "加入一个机器人" );
						ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
					}
				} else {
					if (mt_rand ( 0, 1000 ) < 30) {
						
						Tools::mb_LogResult ( "加入一个机器人" );
						ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
					}
				}
			}
		}
	}
	public function mb_RobotAllExitRoom() {
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_User->m_IsRobot == false)
				continue;
			Tools::mb_LogResult ( "退出一个机器人" );
			ZoneHandler::mb_GetInstance ()->mb_RobotExitRoom ( $this->m_RoomId, $player->m_User );
		}
	}
	public function mb_RobotAutoExitRoom() {
		if ($this->m_RoomType != RoomBase::TYPE_FREE)
			return;
		
		if ($this->m_Algo->mb_GetPlayerCount () == Algorithm::MAXUSER && $this->m_Algo->mb_GetRealPlayerCount () == Algorithm::MAXUSER) {
		} else if ($this->m_Algo->mb_GetPlayerCount () == Algorithm::MAXUSER && $this->m_Algo->mb_GetRealPlayerCount () != Algorithm::MAXUSER) {
			foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
				if (isset ( $player ) && $player->m_User->m_IsRobot == true) {
					ZoneHandler::mb_GetInstance ()->mb_RobotExitRoom ( $this->m_RoomId, $player->m_User );
					break;
				}
			}
		} else {
			if ($this->m_Algo->mb_GetPlayerCount () != $this->m_Algo->mb_GetRealPlayerCount ()) {
				Tools::mb_LogResult ( "准备退出一个机器人" );
				foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
					if (isset ( $player ) == false)
						continue;
					if ($player->m_User->m_IsRobot == false)
						continue;
					if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
						if (mt_rand ( 0, 1000 ) < 850) // 不退出
							break;
					} else {
						if ($this->m_Algo->mb_GetPlayerCount () == 1) {
							if (mt_rand ( 0, 1000 ) < 750) // 不退出
								break;
						} else {
							if (mt_rand ( 0, 1000 ) < 970) // 不退出
								break;
						}
					}
					
					Tools::mb_LogResult ( "退出一个机器人" );
					ZoneHandler::mb_GetInstance ()->mb_RobotExitRoom ( $this->m_RoomId, $player->m_User );
				}
			}
		}
	}
	public function mb_UpdateRoomSetting($params) {
		if (isset ( $params ['minenter'] )) {
			$this->m_Algo->m_MinEnter = $params ['minenter'];
		}
	}
	public function mb_GetRoomConfig() {
		return array (
				'difficulty' => ( int ) $this->m_Algo->m_Difficulty,
				'controlid' => ( string ) $this->m_ControlId,
				'controldifficulty' => ( int ) $this->m_ControlDifficulty,
				'choushui' => ( string ) $this->m_Algo->m_ChouShui,
				'robotcount' => ( string ) $this->m_MaxRobotCount,
				'userchoushui' => ( string ) $this->m_TotalUserChouShui,
				'robotchoushui' => ( string ) $this->m_TotalRobotChouShui,
				'win' => ( string ) $this->m_TotalWin 
		
		);
	}
	public function mb_SetRoomConfig($params) {
		if (isset ( $params ['difficulty'] )) {
			$this->m_Algo->m_Difficulty = $params ['difficulty'];
		}
		if (isset ( $params ['controlid'] ) && isset ( $params ['controldifficulty'] )) {
			$this->m_ControlId = $params ['controlid'];
			$this->m_ControlDifficulty = $params ['controldifficulty'];
			
			$players = $this->m_Algo->mb_GetAllPlayers ();
			for($i = 0; $i < count ( $players ); $i ++) {
				if (isset ( $players [$i] )) {
					$players [$i]->mb_UpdateControlInfo ();
				}
			}
		}
		if (isset ( $params ['choushui'] )) {
			$this->m_Algo->m_ChouShui = $params ['choushui'];
		}
		if (isset ( $params ['robotcount'] )) {
			$this->m_MaxRobotCount = $params ['robotcount'];
		}
		if ($this->m_RoomType == RoomBase::TYPE_FREE)
			ZoneHandler::mb_GetInstance ()->mb_CreateOrUpdateRoomInfo ( $this->m_ZoneId, $this->m_RoomNum, $this->m_Round, $this->m_Algo->m_Difficulty, $this->m_Algo->m_ChouShui, $this->m_TotalUserChouShui, $this->m_TotalRobotChouShui, $this->m_TotalWin, $this->m_ControlId, $this->m_ControlDifficulty, $this->m_MaxRobotCount );
	}
	public function mb_ResetRound() {
		if ($this->m_CurStatus == self::STATUS_WAIT) {
			$this->m_Round = 0;
			$this->mb_EnterWait ();
		} else {
			$this->m_IsResetRound = true;
		}
	}
	// -------------------------------------------状态----------------------------------------//
	public function mb_Start() {
		// 申请一个机器人进入房间
		if ($this->m_RoomType == RoomBase::TYPE_FREE) {
			
			// $count = mt_rand ( 1, 6 );
			for($i = 0; $i < $this->m_MaxRobotCount; $i ++) {
				ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
			}
			
			// ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
			// ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
			// ZoneHandler::mb_GetInstance ()->mb_RobotEnterRoom ( $this->m_RoomId );
		} else if ($this->m_RoomType == RoomBase::TYPE_BUDDY) {
		}
		$this->mb_EnterWait ();
	}
	public function mb_Update() {
		// Tools::mb_LogResult ( "$this->m_RoomName 执行Update方法，当前状态是：$this->m_CurStatus ，当前时间：$this->m_CurTime" );
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_User->m_IsRobot) {
				$players [$i]->mb_Update ();
			}
		}
		
		// if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
		// $this->mb_RobotAutoEnterRoom ();
		// $this->mb_RobotAutoExitRoom ();
		// }
		
		if ($this->m_MaxRobotCount == 0) {
			$this->mb_RobotAllExitRoom ();
		} else {
			if (mt_rand ( 0, 1000 ) < 500)
				$this->mb_RobotAutoEnterRoom ();
			else
				$this->mb_RobotAutoExitRoom ();
		}
		
		switch ($this->m_CurStatus) {
			case self::STATUS_WAIT :
				$this->mb_UpdateWait ();
				break;
			case self::STATUS_QIANGZHUANG :
				$this->mb_UpdateQiangZhuang ();
				break;
			case self::STATUS_DOWNBET :
				$this->mb_UpdateDownBet ();
				break;
			case self::STATUS_OPEN :
				$this->mb_UpdateOpen ();
				break;
			case self::STATUS_OVER :
				$this->mb_UpdateOver ();
				break;
			default :
				break;
		}
	}
	public function mb_EnterWait() {
		Tools::mb_LogResult ( $this->m_RoomName . " 进入等待状态" );
		
		if ($this->m_IsResetRound) {
			$this->m_IsResetRound = false;
			$this->m_Round = 0;
		}
		
		$this->m_CurStatus = self::STATUS_WAIT;
		$this->m_CurTime = 5;
		$this->m_Round ++;
		$this->mb_PushStatusChange ();
		// $this->mb_RobotAutoExitRoom ();
		
		$this->m_Algo->mb_ReStart ();
	}
	private function mb_UpdateWait() {
		if (-- $this->m_CurTime <= 0) {
			// if ($this->m_CurTime < - 2) {
			// if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
			// $this->mb_RobotAutoEnterRoom ();
			// }
			// }
			// if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
			// $this->mb_RobotAutoEnterRoom ();
			// $this->mb_RobotAutoExitRoom ();
			// }
			
			if ($this->m_Algo->mb_GetReadyPlayerCount () >= 2) {
				if ($this->m_Algo->mb_GetRealPlayerCount () > 0) {
					$this->mb_EnterQiangZhuang ();
				} else {
					if ($this->m_CurTime <= - 120) {
						$this->m_Round ++;
						$this->m_CurTime = 0;
					}
				}
			}
		}
		// else if ($this->m_CurTime == 2) {
		// $this->mb_RobotAutoEnterRoom ();
		// }
	}
	public function mb_EnterQiangZhuang() {
		Tools::mb_LogResult ( $this->m_RoomName . " 进入抢庄状态" );
		
		$this->m_Algo->mb_SendPokers ();
		$this->mb_PushPlayersPokers ();
		
		$this->m_CurStatus = self::STATUS_QIANGZHUANG;
		$this->m_CurTime = 10;
		$this->mb_PushStatusChange ();
		
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_User->m_IsRobot) {
				$players [$i]->mb_EnterQiangZhuang ();
			}
		}
	}
	private function mb_UpdateQiangZhuang() {
		if (-- $this->m_CurTime == 0) {
			foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
				if (isset ( $player ) && $player->m_IsPlaying && $player->m_IsQiangZhuang == false) {
					// 时间到，不抢
					$this->m_Algo->mb_QiangZhuang ( $player->m_User, 0 );
				}
			}
			// $this->mb_EnterDownBet ();
		}
	}
	public function mb_EnterDownBet() {
		$bankers = $this->m_Algo->mb_ConfirmBanker ();
		$this->m_Algo->mb_DifficultyMechanism ();
		$this->mb_PushSyncPokers ();
		$this->mb_PushConfirmBanker ( $bankers );
		
		Tools::mb_LogResult ( $this->m_RoomName . " 进入下注状态" );
		
		$this->m_CurStatus = self::STATUS_DOWNBET;
		$this->m_CurTime = 10;
		$this->mb_PushStatusChange ();
		
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_User->m_IsRobot) {
				$players [$i]->mb_EnterDownBet ();
			}
		}
	}
	private function mb_UpdateDownBet() {
		if (-- $this->m_CurTime == 0) {
			foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
				if (isset ( $player ) && $player->m_IsPlaying && $player->m_IsBanker == false && $player->m_IsBet == false) {
					// 时间到，还没下注的自动下注5倍
					$this->m_Algo->mb_DownBet ( $player->m_User, 5 );
				}
			}
			// $this->mb_EnterOpen ();
		}
	}
	public function mb_EnterOpen() {
		// 计算得分
		$this->m_Algo->mb_CalculateScore ();
		$this->mb_PushCalculateScore ();
		
		Tools::mb_LogResult ( $this->m_RoomName . " 进入开牌状态" );
		
		$this->m_CurStatus = self::STATUS_OPEN;
		$this->m_CurTime = 10;
		$this->mb_PushStatusChange ();
		
		$this->m_CurTime += 2;
		
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_User->m_IsRobot) {
				$players [$i]->mb_EnterOpenPoker ();
			}
		}
	}
	private function mb_UpdateOpen() {
		if (-- $this->m_CurTime == 0) {
			$this->mb_EnterOver ();
		} else if ($this->m_CurTime == 2) {
			// 检查所有玩家哪个玩家还没开牌，遍历一下，下发到客户端
			$players = $this->m_Algo->mb_GetAllPlayers ();
			for($i = 0; $i < count ( $players ); $i ++) {
				if (isset ( $players [$i] ) && $players [$i]->m_IsPlaying && $players [$i]->m_IsOpen == false) {
					Tools::mb_LogResult ( $players [$i]->m_User->m_Name . ' 玩家还没开牌' );
					$this->m_Algo->mb_OpenPoker ( $players [$i]->m_User );
				}
			}
		}
	}
	public function mb_EnterOver() {
		Tools::mb_LogResult ( $this->m_RoomName . " 进入结算状态" );
		
		if ($this->m_RoomType == RoomBase::TYPE_FREE)
			ZoneHandler::mb_GetInstance ()->mb_CreateOrUpdateRoomInfo ( $this->m_ZoneId, $this->m_RoomNum, $this->m_Round, $this->m_Algo->m_Difficulty, $this->m_Algo->m_ChouShui, $this->m_TotalUserChouShui, $this->m_TotalRobotChouShui, $this->m_TotalWin, $this->m_ControlId, $this->m_ControlDifficulty, $this->m_MaxRobotCount );
		
		$this->m_CurStatus = self::STATUS_OVER;
		$this->m_CurTime = 10;
		$this->mb_PushStatusChange ();
	}
	private function mb_UpdateOver() {
		if (-- $this->m_CurTime == 0) {
			
			$players = $this->m_Algo->mb_GetAllPlayers ();
			for($i = 0; $i < count ( $players ); $i ++) {
				if (isset ( $players [$i] ))
					$players [$i]->mb_SaveData ();
			}
			
			$this->mb_EnterWait ();
		}
	}
	// -------------------------------------------推送----------------------------------------//
	/**
	 * 推送状态改变
	 */
	public function mb_PushStatusChange() {
		$send = json_encode ( array (
				'status' => $this->m_CurStatus,
				'time' => $this->m_CurTime 
		) );
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushStatusChange', ErrCode_Normal, $send );
		}
	}
	/**
	 * 推送玩家手牌
	 */
	public function mb_PushPlayersPokers() {
		$pokers = array ();
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] ) && $players [$i]->m_IsPlaying) {
				$pokers [] = $players [$i]->m_Pokers;
			} else {
				$pokers [] = array ();
			}
		}
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayersPokers', ErrCode_Normal, json_encode ( $pokers ) );
		}
	}
	/**
	 * 推送玩家抢庄
	 *
	 * @param unknown $name        	
	 * @param unknown $bankerMul        	
	 */
	public function mb_PushPlayerQiangZhuang($name, $bankerMul) {
		$send = array (
				'name' => $name,
				'mul' => ( int ) $bankerMul 
		);
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayerQiangZhuang', ErrCode_Normal, json_encode ( $send ) );
		}
	}
	
	/**
	 * 推送确认庄家
	 *
	 * @param string[] $bankers        	
	 */
	public function mb_PushConfirmBanker($bankers) {
		$banker = $this->m_Algo->mb_GetBanker ();
		$send = array (
				'bankers' => $bankers,
				'name' => $banker->m_User->m_Name,
				'mul' => ( int ) $banker->m_BankerMul 
		);
		
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushConfirmBanker', ErrCode_Normal, json_encode ( $send ) );
		}
	}
	/**
	 * 推送玩家下注
	 *
	 * @param unknown $name        	
	 * @param unknown $betMul        	
	 */
	public function mb_PushPlayerDownBet($name, $betMul) {
		$send = array (
				'name' => $name,
				'mul' => ( int ) $betMul 
		);
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayerDownBet', ErrCode_Normal, json_encode ( $send ) );
		}
	}
	/**
	 *
	 * @param string $name        	
	 */
	public function mb_PushPlayerOpenPoker($name) {
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayerOpenPoker', ErrCode_Normal, $name );
		}
	}
	/**
	 * 推送玩家分数
	 */
	public function mb_PushCalculateScore() {
		$send = array ();
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if ($players [$i] == false || $players [$i]->m_IsPlaying == false) {
				$send [] = null;
			} else {
				$send [] = array (
						'cardtype' => $players [$i]->m_CardType,
						'pokers' => $players [$i]->m_CleanUpPokers,
						'score' => ( double ) $players [$i]->m_RoundPoint 
				);
			}
		}
		
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushCalculateScore', ErrCode_Normal, json_encode ( $send ) );
		}
	}
	/**
	 *
	 * @param Player $newPlayer        	
	 */
	public function mb_PushPlayerIn($newPlayer) {
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			if ($player->m_User->m_Name == $newPlayer->m_User->m_Name)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayerIn', ErrCode_Normal, json_encode ( $newPlayer->mb_GetSyncData () ) );
		}
	}
	/**
	 *
	 * @param string $name        	
	 */
	public function mb_PushPlayerOut($seatId) {
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPlayerOut', ErrCode_Normal, ( string ) $seatId );
		}
	}
	/**
	 *
	 * @param User $user        	
	 */
	public function mb_PushPlayerExit($user) {
		$this->mb_DataOut ( $user, 'PushPlayerExit', ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 */
	public function mb_PushPokersChange() {
		$pokers = array ();
		$players = $this->m_Algo->mb_GetAllPlayers ();
		for($i = 0; $i < count ( $players ); $i ++) {
			if (isset ( $players [$i] )) {
				$pokers [] = $players [$i]->m_Pokers;
			} else {
				$pokers [] = null;
			}
		}
		
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushPokersChange', ErrCode_Normal, json_encode ( $pokers ) );
		}
	}
	/**
	 * 同步扑克牌
	 */
	public function mb_PushSyncPokers() {
		foreach ( $this->m_Algo->mb_GetAllPlayers () as $player ) {
			if (isset ( $player ) == false)
				continue;
			if ($player->m_IsOnline == false || $player->m_User->m_IsRobot == true)
				continue;
			if ($player->m_IsPlaying == false)
				continue;
			$this->mb_DataOut ( $player->m_User, 'PushSyncPokers', ErrCode_Normal, json_encode ( $player->m_Pokers ) );
		}
	}
	// -------------------------------------------枚举----------------------------------------//
	/**
	 * 状态：等待
	 */
	public const STATUS_WAIT = 0;
	/**
	 * 状态：抢庄
	 */
	public const STATUS_QIANGZHUANG = 1;
	/**
	 * 下注
	 */
	public const STATUS_DOWNBET = 2;
	/**
	 * 开牌
	 */
	public const STATUS_OPEN = 3;
	/**
	 * 结算
	 */
	public const STATUS_OVER = 4;
	/**
	 * 牌型
	 */
	public const CARDTYPE_MEINIU = 0;
	public const CARDTYPE_NIU1 = 1;
	public const CARDTYPE_NIU2 = 2;
	public const CARDTYPE_NIU3 = 3;
	public const CARDTYPE_NIU4 = 4;
	public const CARDTYPE_NIU5 = 5;
	public const CARDTYPE_NIU6 = 6;
	public const CARDTYPE_NIU7 = 7;
	public const CARDTYPE_NIU8 = 8;
	public const CARDTYPE_NIU9 = 9;
	public const CARDTYPE_NIUNIU = 10;
	public const CARDTYPE_SHUNZINIU = 11;
	public const CARDTYPE_WUHUANIU = 12;
	public const CARDTYPE_TONGHUANIU = 13;
	public const CARDTYPE_HULUNIU = 14;
	public const CARDTYPE_ZHADANNIU = 15;
	public const CARDTYPE_WUXIAONIU = 16;
	public const CARDTYPE_TONGHUASHUN = 17;
	public const CARDTYPE_NAMES = array (
			'没牛',
			'牛一',
			'牛二',
			'牛三',
			'牛四',
			'牛五',
			'牛六',
			'牛七',
			'牛八',
			'牛九',
			'牛牛',
			'顺子牛',
			'五花牛',
			'同花牛',
			'葫芦牛',
			'炸弹牛',
			'五小牛',
			'同花牛' 
	);
	
	/**
	 * 难度
	 */
	public const DIFF_EASY = 0;
	public const DIFF_NORMAL = 1;
	public const DIFF_DIFFICULTY = 2;
	public const DIFF_SUPERHARD = 3;
	/**
	 * 获取私有成员
	 *
	 * @param unknown $property_name        	
	 * @return unknown|NULL
	 */
	public function __get($property_name) {
		if (isset ( $this->$property_name )) {
			return ($this->$property_name);
		} else {
			return (NULL);
		}
	}
	public function __set($property_name, $val) {
		$this->$property_name = $val;
	}
}